package com.kefan.reptilemanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kefan.reptilemanagement.common.entities.Spider;
import com.kefan.reptilemanagement.common.entities.SysUser;
import com.kefan.reptilemanagement.common.entities.Task;
import com.kefan.reptilemanagement.common.errorHandler.BizException;
import com.kefan.reptilemanagement.mapper.TaskMapper;
import com.kefan.reptilemanagement.service.QueueServer;
import com.kefan.reptilemanagement.service.SpiderService;
import com.kefan.reptilemanagement.service.SysUserService;
import com.kefan.reptilemanagement.service.TaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kefan.reptilemanagement.utils.RabbitUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sjj
 * @since 2024-05-03
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {

    @Autowired
    SpiderService spiderService;
    @Autowired
    RabbitUtils rabbitUtils;
    @Autowired
    QueueServer queueServer;
    @Autowired
    SysUserService sysUserService;
    @Override
    public Task add(Task task) {
        //1排队2.执行中3.已完成4.失败
        task.setStatus(1);
        task.setStartTime(LocalDateTime.now());
        task.setCreated(LocalDateTime.now());
        task.setUpdated(LocalDateTime.now());
        save(task);
        String message=task.getId().toString()+','+task.getTotalTime();
        if(task.getTotalTime()>30)
        {
            queueServer.sendMessage(message,"fast");
        }
        else
        {
            queueServer.sendMessage(message,"common");
        }
        return task;
    }

    @Override
    public List<Task> getList(Long userId, Integer status, String name) {

        QueryWrapper<Task>wrapper=new QueryWrapper<>();
        if(userId!=null)
        {
            wrapper.eq("user_id",userId);
        }
        if(status!=null)
        {
            wrapper.eq("status",status);
        }
        if(name!=null)
        {
            wrapper.eq("name",name);
        }
        return list(wrapper);
    }

    @Override
    public Task delete(Long id) {
        Task task=getById(id);
        if(task==null)
        {
            throw  new BizException("任务不存在");
        }
        removeById(id);
        return task;
    }

    @Override
    public List<Task> getCurrent() {
        //获取当前用户名
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //获取当前用户
        SysUser sysUser = sysUserService.getByUsername(username);
        QueryWrapper<Task>wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",sysUser.getId());
        return list(wrapper);
    }

    @Override
    public String getResFile(Long id) {
        return getById(id).getResultFile();
    }

}
